package com.library.dao.db;

import com.library.dao.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.library.entity.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcAdminRepository implements AdminRepository {
    private final JdbcTemplate jdbcTemplate;
    private final JdbcOperations jdbc;

    /**
     *
     * @param jdbcTemplate 传入 jdbcTemplate
     * @param jdbc 传入 jdbc
     *             构造方法
     */
    @Autowired
    public JdbcAdminRepository(JdbcTemplate jdbcTemplate, JdbcOperations jdbc) {
        this.jdbcTemplate = jdbcTemplate;
        this.jdbc = jdbc;
    }


    public static class AdminRowMapper implements RowMapper<Admin>{
        /**
         *
         * @param rs
         * @param rowNum
         * @return new admin对象
         * @throws SQLException
         */
        public Admin mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new Admin(
                    rs.getLong("id"),
                    rs.getString("userName"),
                    rs.getString("password"),
                    rs.getString("fullName"),
                    rs.getString("email"),
                    rs.getString("phoneNo"),
                    rs.getBoolean("deleteFlag")
            );
        }
    }

    /**
     *
     * @return admin对象List列表
     */
    @Override
    public List<Admin> findAll(){
        List<Admin> admins=jdbc.query("select * from Admins where deleteFlag=false order by id",new AdminRowMapper());
        return admins.isEmpty()?null:admins;
    }

    /**
     *
     * @param offset 起点
     * @param limit 条数
     * @return admin对象List
     */
    @Override
    public List<Admin> findByPage(int offset,int limit){
        return jdbc.query(
                "select * from Admins where deleteFlag =0 limit ? offset ?",
                new AdminRowMapper(),limit,offset
        );
    }

    /**
     *
     * @param userName 用户名userName
     * @return admin对象
     */
    @Override
    public Admin findByName(String userName){
        List<Admin> admins= jdbc.query("select * from Admins where userName=?",new AdminRowMapper(),userName);
        return admins.isEmpty()?null:admins.get(0);
    }

    /**
     *
     * @return Admin表中的数据条数
     */
    @Override
    public int countAdmins(){
        return jdbc.queryForObject("select count(*) from Admins",Integer.class);
    }

    /**
     *
     * @param admin 传入admin对象，更新该对象在数据库中的信息
     */
    @Override
    public void updateInfo(Admin admin){
        jdbc.update("update Admins set password=?,dullName=?,email=?,phoneNO=?",
                admin.getPassword(),admin.getFullName(),admin.getEmail(),admin.getPhoneNo());
    }

    /**
     *
     * @param userName 传入userName,将该admin的deleteFlag设为true
     */
    @Override
    public void deleteByName(String userName){
        jdbc.update("update Admins set deleteFlag=true where userName=?",userName);
    }

    /**
     *
     * @param admin 传入admin对象，在数据库中添加一条该数据
     */
    @Override
    public void addAdmin(Admin admin) {
        jdbc.update("insert into Admins(userName,password,fullName,email,phoneNo) values(?,?,?,?,?)",
                admin.getUserName(), admin.getPassword(), admin.getFullName(), admin.getUserName(), admin.getPhoneNo()
        );
    }

    /**
     *
    * @param password 传入新password
     * @param id 根据传入的id查找admin
     */
    @Override
    public void updateAdmin(String password,long id){
        jdbc.update("update Admins set password=? where id=?",password,id);
    }
}
